﻿using System;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using System.Diagnostics;

namespace MyCraft
{
    public enum Direction
    {
        Up,
        Down,
        Left,
        Right
    }

    public class Player
    {
        #region Attributs
        private Rectangle _hitBox;

        private Texture2D _texture, _pixel;
        private SpriteFont _font;
        private int _spriteLine, _spriteColumn, _timer, _timerTick;
        private SpriteEffects _effect;
        private Direction _direction;

        private string _name;
        private int _life;
        private Vector2 _nameSize;
        private Rectangle _lifeBarOut, _lifeBar, _nameBar;
        #endregion

        #region Constructeurs
        public Player()
        {
            _hitBox = new Rectangle(0, 0, 15, 16);
            _spriteColumn = 1;
            _spriteLine = 0;
            _direction = Direction.Down;
            _effect = SpriteEffects.None;

            _timer = 0;
            _timerTick = 7;

            _life = 50;
            _name = "Pikachu";
            _nameSize = new Vector2();
            _lifeBarOut = new Rectangle(-6, -6, 100 / 2 + 4, 9);
            _lifeBar = new Rectangle(-4, -4, 100 / 2, 5);
            _nameBar = new Rectangle(-10, -10, 10, 10);

            Move();
        }
        #endregion

        #region Propriétés
        public Rectangle HitBox
        {
            get { return _hitBox; }
        }

        internal Direction Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }

        public int Life
        {
            get { return _life; }
            set { _life = value; }
        }
        #endregion

        #region Methodes
        public void LoadContent(ContentManager content)
        {
            _texture = content.Load<Texture2D>("pikachu");
            _font = content.Load<SpriteFont>("Font");
            _pixel = content.Load<Texture2D>("Pixel");
        }

        private void Move()
        {
            _life = new Random().Next(Math.Max(0, _life - 5), Math.Min(100, _life + 5) + 1);
            Debug.WriteLine("Vie : " + _life);

            _lifeBarOut.X = _hitBox.Center.X - 26;
            _lifeBarOut.Y = _hitBox.Top - 10;

            _lifeBar.X = _lifeBarOut.X + 2;
            _lifeBar.Y = _lifeBarOut.Y + 2;
            _lifeBar.Width = _life / 2;

            _nameSize.X = _lifeBarOut.X;
            _nameSize.Y = _lifeBarOut.Y - 20;
        }
        #endregion

        #region Update & Draw
        public void Update(MouseState mouse, KeyboardState keyboard, GameTime gameTime)
        {
            int speed = 2;
            if ((keyboard.IsKeyDown(Keys.Up) || keyboard.IsKeyDown(Keys.Down) ||
                keyboard.IsKeyDown(Keys.Left) || keyboard.IsKeyDown(Keys.Right))) // && _life>0)
            {
                if(keyboard.IsKeyDown(Keys.Left))
                {
                    _hitBox.X -= speed;
                    _direction = Direction.Left;
                }
                if(keyboard.IsKeyDown(Keys.Right))
                {
                    _hitBox.X += speed;
                    _direction = Direction.Right;
                }
                if(keyboard.IsKeyDown(Keys.Up))
                {
                    _hitBox.Y -= speed;
                    _direction = Direction.Up;
                }
                if(keyboard.IsKeyDown(Keys.Down))
                {
                    _hitBox.Y += speed;
                    _direction = Direction.Down;
                }

                Move();
                _timer++;

                switch (_direction)
                {
                    case Direction.Up:
                        _spriteLine = 1;
                        _effect = SpriteEffects.None;
                        break;
                    case Direction.Down:
                        _spriteLine = 0;
                        _effect = SpriteEffects.None;
                        break;
                    case Direction.Left:
                        _spriteLine = 2;
                        _effect = SpriteEffects.None;
                        break;
                    case Direction.Right:
                        _spriteLine = 2;
                        _effect = SpriteEffects.FlipHorizontally;
                        break;
                }

                if (_timer > _timerTick)
                {
                    if (++_spriteColumn > 2)
                        _spriteColumn = 0;

                    _timer = 0;
                }
            }
        }

        public void Draw(SpriteBatch spritebatch)
        {
            //Bar
            spritebatch.Draw(_pixel, _lifeBarOut, Color.Blue);
            spritebatch.Draw(_pixel, _lifeBar, Color.Red);
            spritebatch.DrawString(_font, _name, _nameSize, Color.Black);

            spritebatch.Draw(_texture, _hitBox,
                new Rectangle(_spriteColumn * 25, _spriteLine * 27, 25, 27),
                Color.White, 0f, Vector2.Zero, _effect, 0f);
        }
        #endregion
    }
}
